//
//  SpecView.m
//  CarHome
//
//  Created by 张世琦 on 14-9-23.
//  Copyright (c) 2014年 WJN_work@163.com. All rights reserved.
//

#import "SpecView.h"
#import "SpecCarModel.h"
#import "BrandModel.h"
#import "FctCarModel.h"
#import "CarModel.h"
#import "FctModel.h"
#import "SpecModel.h"
#import "DeliverModel.h"

NSString *const specCell = @"cellId";

@implementation SpecView

- (void)dealloc
{
    [_letterArray release];
    _letterArray = nil;
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _letterArray = [[NSMutableArray alloc] init];
        [self createTableView];
        
    }
    return self;
}

#pragma mark -
#pragma mark 创建TableView

- (void)createTableView
{
    UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 240, self.frame.size.height - 20)];
    aView .backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    UIView *bView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 240, 44)];
    bView.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, 80, 44)];
    label.text = @"选择车系";
    [bView addSubview:label];
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setFrame:CGRectMake(190, 0, 50, 44)];
    [rightButton setTitle:@"关闭" forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [bView addSubview:rightButton];
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setFrame:CGRectMake(0, 0, 50, 44)];
    [leftButton setTitle:@"返回" forState:UIControlStateNormal];
    [leftButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
    [bView addSubview:leftButton];
    [aView addSubview:bView];
    UIButton *all = [UIButton buttonWithType:UIButtonTypeCustom];
    [all setFrame:CGRectMake(0, 0, 320, 40)];
    [all setTitle:@"全部车型" forState:UIControlStateNormal];
    [all setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [all addTarget:self action:@selector(allAction:) forControlEvents:UIControlEventTouchUpInside];
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 74, 240, self.frame.size.height - 69 - 300) style:UITableViewStylePlain];
    _table.tableHeaderView = all;
    [_table registerClass:[UITableViewCell class] forCellReuseIdentifier:specCell];
    _table.dataSource = self;
    _table.delegate = self;
    [aView addSubview:_table];
    [self addSubview:aView];
}

- (void)allAction:(id)sender
{
    DeliverModel *model = [[DeliverModel alloc] init];
    model.fctId = self.fctId;
    model.specId = self.specId;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"chexing" object:model];
    [self.superview.superview removeFromSuperview];
}

#pragma mark -
#pragma mark 关闭, 返回按钮点击事件

- (void)buttonAction:(id)sender
{
    [self.superview.superview removeFromSuperview];
}

- (void)button:(id)sender
{
    [self removeFromSuperview];
}

#pragma mark -
#pragma mark UITableViewDataSource协议

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    SpecCarModel *specCar = [_letterArray objectAtIndex:section];
    return [specCar.list count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:specCell forIndexPath:indexPath];
    SpecCarModel *specCar = [_letterArray objectAtIndex:indexPath.section];
    SpecModel *spec = [specCar.list objectAtIndex:indexPath.row];
    [cell.textLabel setText:spec.name];
    return cell;
}

//section数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_letterArray count];
}

//section标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    SpecCarModel *specCar = [_letterArray objectAtIndex:section];
    return specCar.letter;
}

#pragma mark -
#pragma mark UITableViewDelegate协议

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SpecCarModel *specCar = [_letterArray objectAtIndex:indexPath.section];
    SpecModel *spec = [specCar.list objectAtIndex:indexPath.row];
    NSString *url = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.2/dealer/pdspecs-a2-pm1-v4.2.0-pi0-c0-o0-b%ld-ss%ld-sp%ld-p1-s20.html", self.fctId, self.specId, spec.carId];
    DeliverModel *model = [[DeliverModel alloc] init];
    model.url = url;
    model.carId = spec.carId;
    model.fctId = self.fctId;
    model.specId = self.specId;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"center" object:model];
    [self.superview.superview removeFromSuperview];
}

#pragma mark -
#pragma mark 网络请求

- (void)conncetion:(NSString *)url
{
    [NetUrl connect:url parmater:nil Block:^(NSData *data) {
        NSMutableDictionary *dic = (NSMutableDictionary *)data;
        NSMutableDictionary *result = [dic objectForKey:@"result"];
        NSMutableArray *list = [result objectForKey:@"list"];
        for (NSMutableDictionary *dic in list) {
            SpecCarModel *specCar = [[SpecCarModel alloc] init];
            specCar.letter = [dic objectForKey:@"name"];
            NSMutableArray *speclist = [dic objectForKey:@"speclist"];
            for (NSMutableDictionary *dic in speclist) {
                SpecModel *spec = [[SpecModel alloc] init];
                spec.name = [dic objectForKey:@"name"];
                spec.carId = [[dic objectForKey:@"id"]intValue];
                [specCar.list addObject:spec];
            }
            [_letterArray addObject:specCar];
        }
        [_table reloadData];
    }];
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
